package sfin.swt.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.annotation.Resource;
import javax.naming.NamingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.springframework.beans.factory.annotation.Autowired;

import sfin.controller.BancoController;
import sfin.controller.CartaoController;
import sfin.controller.CategoriaController;
import sfin.controller.ConfigController;
import sfin.controller.ContaController;
import sfin.controller.ControllerUtil;
import sfin.controller.EstatisticaController;
import sfin.controller.LancamentoController;
import sfin.controller.PeriodoController;
import sfin.controller.SimulacaoController;
import sfin.repository.LancamentoRepository;
import sfin.service.BancoService;
import sfin.service.CartaoService;
import sfin.service.CategoriaService;
import sfin.service.ConfigService;
import sfin.service.ContaService;
import sfin.service.LancamentoService;
import sfin.service.ListService;
import sfin.service.PeriodoService;
import sfin.service.SimulacaoService;
import sfin.swt.composite.EstatisticaComposite;
import sfin.swt.data.LancamentoTableData;
import sfin.swt.events.ServiceChangeEvent;
import sfin.swt.events.ServiceChangeListener;
import sfin.swt.exception.StatizoException;
import sfin.swt.filter.LancamentoFilter;
import snoghert.db.util.impl.PersistenceUtilType;
import snoghert.db.util.impl.SnoghertPersistenceUtil;
import br.com.statizo.financeiro.core.repository.impl.RepositoryFactory;
import br.com.statizo.financeiro.domain.enums.GrupoLancamento;

public class ApplicationRunner implements Runnable, ServiceChangeListener{

	private Display display;
	private Shell shell;
	private TabFolder tabFolder;
	private LayoutUtil layoutUtil;

	private Table lancTable;

	private LancamentoController lancamentoController;
	private PeriodoController periodoController; 
	private BancoController bancoController;
	private CartaoController cartaoController;

	private ResourceBundle bundle;
	private Locale locale;

	private LancamentoFilter lancFilter;

	private Label lancTableCountLbl;
	private Label lancTableSumLbl;
	private CategoriaController categoriaController;

	private ControllerUtil controllerUtil;
	protected ContaController contaController;

	private Composite root;

	private ToolBar toolBar;
	private Label secondToolBarSeparator;
	private ToolItem pgToolItem;
	private ToolItem npgToolItem;
	private ToolItem cardToolItem;
	private ToolItem ncardToolItem;
	private ToolItem entradaToolItem;
	private ToolItem saidaToolItem;
	private ToolBar secondToolBar;
	private Label firstToolBarSeparator;
	private PeriodoService periodoService;
	private DialogUtil dialogUtil;
	private ToolItem resumoItem;

	private FormatterUtil formatterUtil;
	private Combo cbMonth;
	private Combo cbYear;

	private MessageUtil messageUtil;

	private SnoghertPersistenceUtil persistenceUtil;
	private SimulacaoController simulacaoController;
	private ConfigController configController;
	
	private SelectionListener entryListener;
	private SelectionListener bankListener;
	private ToolItem entryItens;
	private SelectionListener lastClickedListener;
	private SelectionListener categListener;
	private SelectionListener currAccountListener;
	private SelectionListener creditCardListener;
	//private Image odfImage;
	private Image exitImage;
	private ToolItem gerarSaldoItem;
	private ToolItem liberarSaldoItem;
	private Image balanceArrowIcon;

	private Image entryImage;
	private Image bankImage;
	private Image categoryImage;
	private Image currAccountImage;
	private Image creditCardImage;
	private Image paidImage;
	private Image notPaidImage;
	private Image receitaImage;
	private Image despesaImage;
	private ToolItem moneyToolItem;
	
	private java.util.List<ToolItem> filtroFormaPgtoList = new ArrayList<ToolItem>();
	private ToolItem checkToolItem;
	private ToolItem transfToolItem;
	private Combo contasCombo;
	private Image taskedImage;
	private ToolItem taskedToolItem;
	private Image logotipoIcon;
	private ApplicationUtil applicationUtil;
	private EstatisticaController estatisticaController;
	
	@Autowired
	private InjectionUtil injectionUtil;
	
	@Resource
	private LancamentoRepository lancamentoRepository;
	
	public ApplicationRunner(){
		this(null,null);
	}

	public ApplicationRunner(String language, String country) {
		if(language == null || country == null){
			this.locale = new Locale("pt","BR");
		}else{
			this.locale = new Locale(language, country);			
		}
	}

	private Properties getConfigFile() throws URISyntaxException, IOException{
		lancamentoRepository.findAll();
		File file = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
		file = new File(file.getParentFile(), "statizo.ini");
		InputStream is = new FileInputStream(file);
		Properties properties = new Properties();
		properties.load(is);
		return properties;
	}
	
	private void initalizeContexts() throws URISyntaxException {

		GrupoLancamento.resourceBundle = bundle;

		Properties configs = null;
		
		try{
			configs = getConfigFile();
		}catch(Exception e){
//			MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
//			messageBox.setText("ERROR");
//			messageBox.setMessage(e.getMessage());
//			messageBox.open();
//			return;
		}
		
		//Util
//		persistenceUtil = new SnoghertPersistenceUtil(configs.getProperty("data.location"));
		persistenceUtil = new SnoghertPersistenceUtil(PersistenceUtilType.MYSQL);
		formatterUtil = new FormatterUtil(locale);
		messageUtil = new MessageUtil(bundle, shell);
		layoutUtil = new LayoutUtil();
		controllerUtil = new ControllerUtil(bundle);

		RepositoryFactory repositoryFactory = new RepositoryFactory(this.persistenceUtil);

		//Services
		periodoService = new PeriodoService(bundle, formatterUtil);
		BancoService bancoService = new BancoService(bundle, formatterUtil, repositoryFactory.getBancoRepository());
		ContaService contaService = new ContaService(bundle, formatterUtil, repositoryFactory.getContaRepository());
		CategoriaService categoriaService = new CategoriaService(bundle, formatterUtil, repositoryFactory.getCategoriaRepository());
		CartaoService cartaoService = new CartaoService(bundle, formatterUtil, repositoryFactory.getCartaoRepository());
		ConfigService configService = new ConfigService(bundle, formatterUtil, repositoryFactory.getColorConfigRepository(), 
				repositoryFactory.getConfigRepo(), periodoService);
		LancamentoService lancamentoService = new LancamentoService(bundle, formatterUtil,
				repositoryFactory.getLancamentoRepository(),periodoService, contaService, configService);
		
		
		SimulacaoService simulacaoService = new SimulacaoService(bundle, formatterUtil, lancamentoService, periodoService);

		//Controllers
		periodoController = new PeriodoController(periodoService);
		bancoController = new BancoController(bancoService);
		contaController = new ContaController(contaService, bancoService, controllerUtil);
		categoriaController = new CategoriaController(categoriaService);
		cartaoController = new CartaoController(cartaoService, contaService, controllerUtil);
		configController = new ConfigController(configService);

		lancamentoController = new LancamentoController(bundle, controllerUtil);
		lancamentoController.setLancamentoService(lancamentoService);
		lancamentoController.setCategoriaService(categoriaService);
		lancamentoController.setCartaoService(cartaoService);
		lancamentoController.setContaService(contaService);
		simulacaoController = new SimulacaoController(simulacaoService, periodoService);

		estatisticaController = new EstatisticaController(categoriaService, lancamentoService);
		
		//Services relations
		contaService.setBancoService(bancoService);
		cartaoService.setContaService(contaService);
		lancamentoService.setCategoriaService(categoriaService);
		lancamentoService.setCartaoService(cartaoService);

		//Toolbar Filter
		lancFilter = new LancamentoFilter();
		lancamentoController.setFilter(lancFilter);

		//Dialog to open edit composites
		dialogUtil = new DialogUtil(bundle, display, messageUtil);
		dialogUtil.setBancoController(bancoController);
		dialogUtil.setCartaoController(cartaoController);
		dialogUtil.setCategoriaController(categoriaController);
		dialogUtil.setContaController(contaController);
		dialogUtil.setLancamentoController(lancamentoController);
		dialogUtil.setSimulacaoController(simulacaoController);
		dialogUtil.setConfigController(configController);

		//Listening
		lancamentoService.addListener(this);
		periodoService.addListener(lancamentoService);
		contaService.addListener(lancamentoService);
		contaService.addListener(this);
		configService.addListener(this);
		
	}

	protected void exit() throws NamingException{
		persistenceUtil.close();
		display.dispose();
		System.exit(0);
	}

	protected void restart(String language, String contry){
		try {
			persistenceUtil.close();
			display.dispose();
			new Thread(new ApplicationRunner(language, contry)).run();
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	protected void initSecondToolBar(){

		secondToolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
		FormData formData = new FormData();
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.top = new FormAttachment(firstToolBarSeparator,2);
		secondToolBar.setLayoutData(formData);

		new ToolItem(secondToolBar,SWT.SEPARATOR);

		ToolItem toolItem = new ToolItem(secondToolBar, SWT.PUSH);
		toolItem.setImage(ResourceUtil.getImage(display, "date-previous-icon.png"));
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				periodoController.back();
			}
		});

		toolItem = new ToolItem(secondToolBar, SWT.SEPARATOR);
		cbMonth = new Combo(secondToolBar,SWT.BORDER);
		cbMonth.setItems(periodoController.getMonths());
		cbMonth.select( periodoController.getSelectedMonth() );
		cbMonth.pack();
		toolItem.setWidth(cbMonth.getBounds().width);
		toolItem.setControl(cbMonth);

		cbMonth.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Combo cb = (Combo) e.widget;
				periodoController.setSelectedMonth( cb.getSelectionIndex() );
			}
		});

		new ToolItem(secondToolBar, SWT.SEPARATOR);

		toolItem = new ToolItem(secondToolBar, SWT.SEPARATOR);
		cbYear = new Combo(secondToolBar,SWT.BORDER);
		cbYear.setItems(periodoController.getYears() );
		cbYear.select( periodoController.getSelectedYear() );
		cbYear.pack();
		toolItem.setWidth(cbYear.getBounds().width);
		toolItem.setControl(cbYear);

		cbYear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Combo cb = (Combo) e.widget;
				periodoController.setSelectedYear(cb.getSelectionIndex());
			}
		});

		toolItem = new ToolItem(secondToolBar, SWT.PUSH);
		toolItem.setImage(ResourceUtil.getImage(display, "date-next-icon.png"));
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				periodoController.next();
			}
		});

		new ToolItem(secondToolBar, SWT.SEPARATOR);

		toolItem = new ToolItem(secondToolBar, SWT.SEPARATOR);
		contasCombo = new Combo(secondToolBar,SWT.BORDER);
		refreshContasCombo();
		
		toolItem.setWidth(contasCombo.getBounds().width);
		toolItem.setControl(contasCombo);

		
		contasCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Combo cb = (Combo) e.widget;
				contaController.setApplicationSelectedIndex(cb.getSelectionIndex());
				fillData();
			}
		});

		
		new ToolItem(secondToolBar, SWT.SEPARATOR); 

		gerarSaldoItem = new ToolItem(secondToolBar, SWT.PUSH);
		gerarSaldoItem.setImage(balanceArrowIcon);
		gerarSaldoItem.setToolTipText(bundle.getString("lancamento.saldo.enviar-proximo-mes"));
		gerarSaldoItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.gerarSaldo();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}

			}
		});

		new ToolItem(secondToolBar, SWT.SEPARATOR);

		liberarSaldoItem = new ToolItem(secondToolBar, SWT.PUSH);
		liberarSaldoItem.setImage(ResourceUtil.getImage(display, "arrow-return-180-icon.png"));
		liberarSaldoItem.setToolTipText(bundle.getString("lancamento.saldo.liberar"));
		liberarSaldoItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.liberarLancamentos();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});

		new ToolItem(secondToolBar, SWT.SEPARATOR);
		
		ToolItem simulatorItem = new ToolItem(secondToolBar, SWT.PUSH);
		simulatorItem.setImage(ResourceUtil.getImage(display, "Business-Statistics-icon.png"));
		simulatorItem.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					dialogUtil.openSimulacaoDialog();
				}
		});
		

		secondToolBarSeparator = new Label(shell,SWT.SEPARATOR | SWT.HORIZONTAL);
		formData = new FormData();
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.top = new FormAttachment(secondToolBar,2);
		secondToolBarSeparator.setLayoutData(formData);

	}

	
	private void refreshContasCombo() {
		refreshContasCombo(null);
	}
	
	private void refreshContasCombo(ContaService service) {

		contasCombo.setItems( contaController.getNomes() );
		
		if(configController.isIniContaPadrao()){
			contasCombo.select(contaController.getApplicationSelectedIndex());	
		}else{
			
			if(service != null && ((ListService) service ).size() == 1 ){ //Quando acabou de inserir o primeiro.
				contasCombo.select(1);
			}else{
				contasCombo.select(0);
			}

		}
		
		contasCombo.pack();
	}

	protected void initializeResourceImages(){
		entryImage = ResourceUtil.getImage(display, "write.png");
		bankImage = ResourceUtil.getImage(display, "entity-icon.png");
		categoryImage = ResourceUtil.getImage(display, "category.png");
		currAccountImage = ResourceUtil.getImage(display, "current-account.png");
		creditCardImage = ResourceUtil.getImage(display, "credit-card.png");
		paidImage = ResourceUtil.getImage(display, "tag-green-icon.png");
		notPaidImage = ResourceUtil.getImage(display, "tag-red-icon.png");
		taskedImage = ResourceUtil.getImage(display, "sheduled-task-icon.png");
		receitaImage = ResourceUtil.getImage(display, "chart-up-color-icon.png");
		despesaImage = ResourceUtil.getImage(display, "chart-down-color-icon.png");
		//odfImage = ResourceUtil.getImage(display, "odf.png");
		exitImage = ResourceUtil.getImage(display, "Log-Out-icon.png");
		balanceArrowIcon = ResourceUtil.getImage(display, "balance-arrow-icon.png");
		logotipoIcon = ResourceUtil.getImage(display, "logo-min.png");
	}



	private void initializeEvents(){

		entryListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.widget == entryItens){
					if(e.detail != 0){
						return;
					}else{
						dialogUtil.openLancamentoDialog(true);
					}
				}else{
					entryItens.removeSelectionListener(lastClickedListener);
					lastClickedListener = this;
					entryItens.addSelectionListener(this);
					entryItens.setImage(entryImage);
					dialogUtil.openLancamentoDialog(true);
				}

			}
		};

		bankListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.widget == entryItens){
					if(e.detail != 0){
						return;
					}else{
						dialogUtil.openBancoDialog();
					}
				}else{
					entryItens.removeSelectionListener(lastClickedListener);
					lastClickedListener = this;
					entryItens.addSelectionListener(this);
					entryItens.setImage(bankImage);
					dialogUtil.openBancoDialog();
				}
			}
		};

		categListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.widget == entryItens){
					if(e.detail != 0){
						return;
					}else{
						dialogUtil.openCategoriaDialog();
					}
				}else{
					entryItens.removeSelectionListener(lastClickedListener);
					lastClickedListener = this;
					entryItens.addSelectionListener(this);
					entryItens.setImage(categoryImage);
					dialogUtil.openCategoriaDialog();
				}
			}
		};

		currAccountListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.widget == entryItens){
					if(e.detail != 0){
						return;
					}else{
						dialogUtil.openContaDialog();
					}
				}else{
					entryItens.removeSelectionListener(lastClickedListener);
					lastClickedListener = this;
					entryItens.addSelectionListener(this);
					entryItens.setImage(currAccountImage);
					dialogUtil.openContaDialog();
				}
			}
		};

		creditCardListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(e.widget == entryItens){
					if(e.detail != 0){
						return;
					}else{
						dialogUtil.openCartaoDialog();
					}
				}else{
					entryItens.removeSelectionListener(lastClickedListener);
					lastClickedListener = this;
					entryItens.addSelectionListener(this);
					entryItens.setImage(creditCardImage);
					dialogUtil.openCartaoDialog();
				}
			}
		};
	}


	protected void clearSelectionFormaPgto(ToolItem except){
		for (int i = 0; i < filtroFormaPgtoList.size(); i++) {
			ToolItem item = filtroFormaPgtoList.get(i);
			if(!item.equals(except))
				filtroFormaPgtoList.get(i).setSelection(false);
		}
		lancFilter.limparFiltroFormaPgto();
	}
	
	
	
	protected void initToolBar(){

		toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);

		entryItens = new ToolItem(toolBar, SWT.DROP_DOWN);
		entryItens.setImage(entryImage);
		entryItens.addSelectionListener(entryListener);
		lastClickedListener = entryListener;

		final Menu menu = new Menu(shell, SWT.POP_UP);

		MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setImage(entryImage);

		menuItem.setText(bundle.getString("string.general.newentry"));
		menuItem.addSelectionListener(entryListener);

		menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setImage(bankImage);
		menuItem.setText(bundle.getString("menu.bank"));
		menuItem.addSelectionListener(bankListener);

		menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setImage(categoryImage);
		menuItem.setText(bundle.getString("menu.categ"));
		menuItem.addSelectionListener(categListener);

		menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setImage(currAccountImage);
		menuItem.setText(bundle.getString("menu.account"));
		menuItem.addSelectionListener(currAccountListener);

		menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setImage(creditCardImage);
		menuItem.setText(bundle.getString("menu.credit_card"));
		menuItem.addSelectionListener(creditCardListener);

		entryItens.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if(event.detail == SWT.ARROW) {
					Rectangle bounds = entryItens.getBounds();
					Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
					menu.setLocation(point);
					menu.setVisible(true);
				}
			}
		});

		FormData formData = new FormData();
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.top = new FormAttachment(0);

		toolBar.setLayoutData(formData);

		toolBar.pack();

		new ToolItem(toolBar, SWT.SEPARATOR);

		pgToolItem = new ToolItem(toolBar, SWT.CHECK);
		pgToolItem.setImage(paidImage);
		pgToolItem.setToolTipText(bundle.getString("toolbar.filter.only-paid"));
		pgToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(pgToolItem.getSelection()){
					npgToolItem.setSelection(false);
					taskedToolItem.setSelection(false);
					lancFilter.setAgend(false);
					lancFilter.setPg(true);
				}else{
					lancFilter.setPg(null);
				}
				fillData();
			}
		});

		npgToolItem = new ToolItem(toolBar, SWT.CHECK);
		npgToolItem.setImage(notPaidImage);
		npgToolItem.setToolTipText(bundle.getString("toolbar.filter.only-not-paid"));
		npgToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(npgToolItem.getSelection()){
					pgToolItem.setSelection(false);
					lancFilter.setPg(false);
				}else{
					lancFilter.setPg(null);
				}
				fillData();
			}
		});

		taskedToolItem = new ToolItem(toolBar, SWT.CHECK);
		taskedToolItem.setImage(taskedImage);
		taskedToolItem.setToolTipText(bundle.getString("toolbar.filter.only-tasked"));
		taskedToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(taskedToolItem.getSelection()){
					pgToolItem.setSelection(false);
					lancFilter.setPg(false);
					lancFilter.setAgend(true);
				}else{
					lancFilter.setPg(null);
					lancFilter.setAgend(false);
				}
				fillData();
			}
		});

		new ToolItem(toolBar, SWT.SEPARATOR);
		
		moneyToolItem = new ToolItem(toolBar, SWT.CHECK);
		moneyToolItem.setImage(ResourceUtil.getImage(display, "money-icon.png"));
		moneyToolItem.setToolTipText(bundle.getString("toolbar.filter.only-money"));
		moneyToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clearSelectionFormaPgto(moneyToolItem);
				if(moneyToolItem.getSelection()){
					lancFilter.setDebito(true);
				}
			fillData();
			}
		});
		
		checkToolItem = new ToolItem(toolBar, SWT.CHECK);
		checkToolItem.setImage(ResourceUtil.getImage(display, "Finance-Check-Book-icon.png"));
		checkToolItem.setToolTipText(bundle.getString("toolbar.filter.only-check"));
		checkToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clearSelectionFormaPgto(checkToolItem);
				if(checkToolItem.getSelection()){
					lancFilter.setCheque(true);
				}
				fillData();
			}
		});
		
		transfToolItem = new ToolItem(toolBar, SWT.CHECK);
		transfToolItem.setImage(ResourceUtil.getImage(display, "Transfer-Document-icon.png"));
		transfToolItem.setToolTipText(bundle.getString("toolbar.filter.only-transfer"));
		transfToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clearSelectionFormaPgto(transfToolItem);
				if(transfToolItem.getSelection()){
					lancFilter.setTranferencia(true);
				}
				fillData();
			}
		});
		
		cardToolItem = new ToolItem(toolBar, SWT.CHECK);
		cardToolItem.setImage(ResourceUtil.getImage(display, "credit-card.png"));
		cardToolItem.setToolTipText(bundle.getString("toolbar.filter.only-card"));
		cardToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clearSelectionFormaPgto(cardToolItem);
				if(cardToolItem.getSelection()){
					lancFilter.setCartao(true);
				}
				fillData();
			}
		});
		
		ncardToolItem = new ToolItem(toolBar, SWT.CHECK);
		ncardToolItem.setImage(ResourceUtil.getImage(display, "no-credit-card.png"));
		ncardToolItem.setToolTipText(bundle.getString("toolbar.filter.only-no-card"));
		ncardToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clearSelectionFormaPgto(ncardToolItem);
				if(ncardToolItem.getSelection()){
					lancFilter.setCartao(false);
				}else{
					lancFilter.setCartao(null);
				}
				fillData();
			}
		});

		//Filtro Forma Pgto.
		filtroFormaPgtoList.add(cardToolItem);
		filtroFormaPgtoList.add(moneyToolItem);
		filtroFormaPgtoList.add(ncardToolItem);
		filtroFormaPgtoList.add(checkToolItem);
		filtroFormaPgtoList.add(transfToolItem);
		
		
		new ToolItem(toolBar, SWT.SEPARATOR);

		entradaToolItem = new ToolItem(toolBar, SWT.CHECK);
		entradaToolItem.setImage(receitaImage);
		entradaToolItem.setToolTipText(bundle.getString("toolbar.filter.only-incomings"));
		entradaToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(entradaToolItem.getSelection()){
					saidaToolItem.setSelection(false);
					lancFilter.setReceita(true);
				}else{
					lancFilter.setReceita(null);
				}
				fillData();
			}
		});

		saidaToolItem = new ToolItem(toolBar, SWT.CHECK);
		saidaToolItem.setImage(despesaImage);
		saidaToolItem.setToolTipText(bundle.getString("toolbar.filter.only-expenses"));
		saidaToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(saidaToolItem.getSelection()){
					entradaToolItem.setSelection(false);
					lancFilter.setReceita(false);
				}else{
					lancFilter.setReceita(null);
				}
				fillData();
			}
		});

		new ToolItem(toolBar, SWT.SEPARATOR);

		resumoItem = new ToolItem(toolBar, SWT.PUSH);
		resumoItem.setImage(ResourceUtil.getImage(display, "Actions-view-statistics-icon.png"));
		resumoItem.setToolTipText(bundle.getString("toolbar.action.resume"));
		resumoItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openResumoFinanceiroDialog();
			}
		});

		new ToolItem(toolBar, SWT.SEPARATOR);  

		ToolItem itemSeparator = new ToolItem(toolBar, SWT.SEPARATOR);

		final Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE | SWT.SEARCH);
		text.setMessage(bundle.getString("entry.table.filter.label.search-text"));
		text.pack();
		itemSeparator.setWidth(text.getBounds().width + 45);
		itemSeparator.setControl(text);
		text.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				lancFilter.setText(text.getText());
				fillData();
			}
		});

		
		firstToolBarSeparator = new Label(shell,SWT.SEPARATOR | SWT.HORIZONTAL);
		formData = new FormData();
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.top = new FormAttachment(toolBar,3);
		firstToolBarSeparator.setLayoutData(formData);

		
		ToolItem teste = new ToolItem(toolBar, SWT.PUSH);
		teste.setText("Gráfico");
		teste.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openDialog(new CompositeInstantiator() {
					@Override
					public void onCloseDialog() {
					}
					@Override
					public Control instantiateIn(Shell dialog) {
						return new EstatisticaComposite(dialog, SWT.NONE, estatisticaController);
//						return new ReceitaVsDespesaChartComposite(dialog, SWT.NONE);
					}
				}, null,DialogUtil.POSITION_1,DialogUtil.POSITION_2,false);
			}
		});
		
		
		toolBar.pack();

	}


	protected void initMenu(){

		shell.setImage(logotipoIcon);
		
		Menu menuBar = new Menu(shell,SWT.BAR);
		shell.setMenuBar(menuBar);

		MenuItem rootMenu = new MenuItem(menuBar, SWT.CASCADE);
		rootMenu.setText(bundle.getString("menu.file"));

		Menu dropDown = new Menu(menuBar);
		rootMenu.setMenu(dropDown);

		MenuItem  item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("string.general.newentry"));
		item.setImage(entryImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openLancamentoDialog(true);
			}
		});
		
		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.bank"));
		item.setImage(bankImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openBancoDialog();
			}
		});

		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.categ"));
		item.setImage(categoryImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openCategoriaDialog();
			}
		});

		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.account"));
		item.setImage(currAccountImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openContaDialog();
			}
		});


		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.credit_card"));
		item.setImage(creditCardImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openCartaoDialog();
			}
		});

		//item = new MenuItem(dropDown, SWT.PUSH);
		//item.setText(bundle.getString("menu.check"));
		//item.setEnabled(false);//FIXME Implementar em outra versão.

		new MenuItem(dropDown, SWT.SEPARATOR);		


//		item = new MenuItem(dropDown, SWT.PUSH);
//		item.setImage(ResourceUtil.getImage(display, "file-extension-pdf-icon.png"));
//		item.setText(bundle.getString("menu.export.pdf"));
//
//		item = new MenuItem(dropDown, SWT.PUSH);
//		item.setText(bundle.getString("menu.export.odf"));
//		item.setImage(odfImage);
//
//		new MenuItem(dropDown, SWT.SEPARATOR);

		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.close"));
		item.setImage(exitImage);
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					exit();
				} catch (NamingException e1) {
					MessageBox message = new MessageBox(shell,SWT.ERROR);
					message.setMessage(e1.getMessage());
					message.setText(e1.getClass().getName());
					message.open();
				}
			}
		});

		rootMenu = new MenuItem(menuBar, SWT.CASCADE);
		rootMenu.setText(bundle.getString("menu.configuration"));

		dropDown = new Menu(menuBar);
		rootMenu.setMenu(dropDown);

		//Idioma
		item = new MenuItem(dropDown, SWT.CASCADE);
		item.setText(bundle.getString("menu.language"));
		item.setImage(ResourceUtil.getImage(display, "language-icon.png"));

		Menu idiomaDropDown = new Menu(menuBar);
		item.setMenu(idiomaDropDown);

		item = new MenuItem(idiomaDropDown, SWT.SEPARATOR);
		
		item = new MenuItem(idiomaDropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.language.ptBr"));
		item.setImage(ResourceUtil.getImage(display, "Brazil-Flag-1-icon.png"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				restart("pt","BR");
			}
		});

		item = new MenuItem(idiomaDropDown, SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "United-States-Flag-1-icon.png"));
		item.setText(bundle.getString("menu.language.en"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				restart("en","US");
			}
		});

		item = new MenuItem(dropDown, SWT.SEPARATOR);

		item = new MenuItem(dropDown, SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "Categories-preferences-system-icon.png"));
		item.setText(bundle.getString("menu.preferences"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openPreferenciasDialog();
			}
		});
		
		
		rootMenu = new MenuItem(menuBar, SWT.CASCADE);
		rootMenu.setText(bundle.getString("menu.help"));

		dropDown = new Menu(menuBar);
		rootMenu.setMenu(dropDown);

		item = new MenuItem(dropDown, SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "help-icon.png"));
		item.setText(bundle.getString("menu.help.contents"));
		
		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.help.sugestion"));

		item = new MenuItem(dropDown, SWT.SEPARATOR);
		
		item = new MenuItem(dropDown, SWT.PUSH);
		item.setText(bundle.getString("menu.help.about"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openAboutDialog(applicationUtil);
			}
		});

	}

	protected void initFolder(){

		tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayout(new FormLayout());

		FormData formData = new FormData();
		formData.top = new FormAttachment(5);
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.bottom = new FormAttachment(100);
		tabFolder.setLayoutData(formData);

		Group group = new Group(tabFolder,SWT.NONE);
		group.setText(bundle.getString("string.general.year"));
		group.setLayout(new FormLayout());
		layoutUtil.setLayoutData(group, 18, 2, 12, 52);

		List list = new List(group, SWT.BORDER | SWT.SINGLE);
		layoutUtil.setLayoutData(list,1, 1, 98, 98);

		list.setItems( periodoController.getYears() );
		list.setSelection(periodoController.getSelectedYear());
		list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List widget = (List) e.widget;
				periodoController.setSelectedYear(widget.getSelectionIndex());
				fillData();
			}
		});


		group = new Group(tabFolder, SWT.NONE);
		group.setText(bundle.getString("string.general.month"));
		group.setLayout(new FormLayout());		
		layoutUtil.setLayoutData(group, 54, 2, 12, 95);

		list = new List(group, SWT.BORDER | SWT.SINGLE);
		layoutUtil.setLayoutData(list,1, 1, 98, 98);

		list.setItems(periodoController.getMonths());
		list.setSelection(periodoController.getSelectedMonth());
		list.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List widget = (List) e.widget;
				periodoController.setSelectedMonth(widget.getSelectionIndex());
				fillData();
			}
		});

	}

	protected void initTable(){

		root = new Composite(shell, SWT.BORDER);
		root.setLayout( new FormLayout() );
		FormData formData = new FormData();
		formData.top = new FormAttachment(secondToolBarSeparator,2);
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.bottom = new FormAttachment(100);
		root.setLayoutData(formData);

		lancTable = new Table(root, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		lancTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(lancTable.getSelectionCount() == 1){
					dialogUtil.openLancamentoDialog(false);
				}
			}
		});

		lancTable.setHeaderVisible(true);
		lancTable.setLinesVisible(true);

		TableColumn[] columns = new TableColumn[8];

		columns[0] = new TableColumn(lancTable, SWT.NONE);
		columns[0].setText(bundle.getString("lancamento.table.header.dtpg"));

		columns[1] = new TableColumn(lancTable, SWT.NONE);
		columns[1].setText(bundle.getString("lancamento.table.header.dtdv"));

		columns[2] = new TableColumn(lancTable, SWT.NONE);
		columns[2].setText(bundle.getString("lancamento.table.header.cate"));

		columns[3] = new TableColumn(lancTable, SWT.NONE);
		columns[3].setText(bundle.getString("lancamento.table.header.desc"));

		columns[4] = new TableColumn(lancTable, SWT.NONE);
		columns[4].setText(bundle.getString("lancamento.table.header.val"));

		columns[5] = new TableColumn(lancTable, SWT.NONE);
		columns[5].setText(bundle.getString("lancamento.table.header.natu"));

		columns[6] = new TableColumn(lancTable, SWT.NONE);
		columns[6].setText(bundle.getString("lancamento.table.header.fopg"));

		columns[7] = new TableColumn(lancTable, SWT.NONE);
		columns[7].setText(bundle.getString("lancamento.table.header.details"));


		fillData();


		for (int i = 0; i < columns.length; i++) {
			columns[i].setWidth(150);
			//columns[i].pack();
		}


		Menu menu = new Menu(lancTable);

		MenuItem item = new MenuItem(menu, SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "delete.png"));
		item.setText(bundle.getString("string.general.delete"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				MessageBox message = new MessageBox(shell,SWT.OK | SWT.CANCEL);
				message.setMessage(bundle.getString("lancamento.del.message.confirm.message"));
				message.setText(bundle.getString("lancamento.del.message.confirm.title"));

				if(message.open() == SWT.OK){
					try {
						lancamentoController.excluir();
					} catch (StatizoException e1) {
						messageUtil.showMessage(e1);
					}
				}
			}
		});

		item = new MenuItem(menu, SWT.PUSH);
		item.setText(bundle.getString("entry.table.action.label"));
		item.setImage(ResourceUtil.getImage(display, "delete-all.png"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				MessageBox message = new MessageBox(shell,SWT.OK | SWT.CANCEL);
				message.setMessage(bundle.getString("lancamento.del.message.confirm.message"));
				message.setText(bundle.getString("lancamento.del.message.confirm.title"));

				if(message.open() == SWT.OK){
					try {
						lancamentoController.forcarExclusao();
					} catch (StatizoException e1) {
						messageUtil.showMessage(e1);
					}
				}
			}
		});

		new MenuItem(menu, SWT.SEPARATOR);

		item = new MenuItem(menu, SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "edit.png"));
		item.setText(bundle.getString("string.general.edit"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dialogUtil.openLancamentoDialog(false);
			}
		});

		new MenuItem(menu, SWT.SEPARATOR);

		item = new MenuItem(menu, SWT.PUSH);
		item.setImage(paidImage);
		item.setText(bundle.getString("string.general.markpaid"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.marcarComoPago();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});

		item = new MenuItem(menu,SWT.PUSH);
		item.setImage(notPaidImage);
		item.setText(bundle.getString("string.general.marknotpaid"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.marcarComoNaoPago();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});

		new MenuItem(menu, SWT.SEPARATOR);

		item = new MenuItem(menu, SWT.PUSH);
		item.setText(bundle.getString("string.general.markagend"));
		item.setImage(ResourceUtil.getImage(display, "calendar-add-icon.png"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.marcarComoAgendado();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});

		item = new MenuItem(menu,SWT.PUSH);
		item.setImage(ResourceUtil.getImage(display, "calendar-delete-icon.png"));
		item.setText(bundle.getString("string.general.marknotagend"));
		item.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					lancamentoController.marcarComoNaoAgendado();
				} catch (StatizoException e1) {
					messageUtil.showMessage(e1);
				}
			}
		});


		lancTable.setMenu(menu);

		formData = new FormData();
		formData.top = new FormAttachment(0);
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.bottom = new FormAttachment(99,-20);
		lancTable.setLayoutData(formData);

		//Status Bar

		final Composite statusBar = new Composite(root, SWT.BORDER | SWT.RIGHT_TO_LEFT);
		RowLayout statusBarLayout = new RowLayout(SWT.HORIZONTAL);
		statusBar.setLayout(statusBarLayout);

		final Label[] infoLabels = new Label[3];

		Label label = new Label(statusBar,SWT.LEFT_TO_RIGHT);
		label.setAlignment(SWT.CENTER);
		RowData rowData = new RowData();
		rowData.width = 180;
		label.setLayoutData(rowData);
		lancTableCountLbl = label;

		updateCountLabel();

		infoLabels[0] = label;		

		rowData = new RowData();

		label = new Label(statusBar,SWT.SEPARATOR | SWT.VERTICAL);
		label.setLayoutData(rowData);

		label = new Label(statusBar,SWT.LEFT_TO_RIGHT);
		label.setAlignment(SWT.CENTER);
		label.setText("Nenhum selecionado");
		rowData = new RowData();
		rowData.width = 180;
		label.setLayoutData(rowData);		
		infoLabels[1] = label;

		label = new Label(statusBar,SWT.SEPARATOR | SWT.VERTICAL);
		rowData = new RowData();
		label.setLayoutData(rowData);

		label = new Label(statusBar,SWT.RIGHT_TO_LEFT);
		label.setAlignment(SWT.CENTER);
		rowData = new RowData();
		rowData.width = 180;
		label.setLayoutData(rowData);		
		lancTableSumLbl = label;
		infoLabels[2] = label;

		lancTableSumLbl.setVisible(false);

		lancTable.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				lancamentoController.setSelectedIndices(lancTable.getSelectionIndices());

				int selectionCount = lancTable.getSelectionCount();

				if(selectionCount == 0){
					infoLabels[1].setText(bundle.getString("lancamento.table.selected.none"));
				}else if(selectionCount == 1){
					infoLabels[1].setText(bundle.getString("lancamento.table.selected.one"));
				}else{
					infoLabels[1].setText(MessageFormat.format(bundle.getString("lancamento.table.selected.many"), selectionCount));					
				}

				double sum = 0.0;
				if(selectionCount > 1){
					sum = lancamentoController.getSum();
					lancTableSumLbl.setVisible(true);
					lancTableSumLbl.setText(MessageFormat.format(bundle.getString("lancamento.table.sum"), Currency.getInstance(locale).getSymbol(),sum));
				}else{
					lancTableSumLbl.setVisible(false);
				}

			}

		});

		statusBar.pack();

		formData = new FormData();
		formData.top = new FormAttachment(lancTable, 1);
		formData.left = new FormAttachment(0);
		formData.right = new FormAttachment(100);
		formData.bottom = new FormAttachment(100);
		statusBar.setLayoutData(formData);


	}

	private void updateCountLabel() {

		if(lancTableCountLbl == null)
			return;

		int itemCount = lancTable.getItemCount();
		if(itemCount == 0){
			lancTableCountLbl.setText(bundle.getString("lancamento.table.empty"));
		}else if(itemCount == 1){
			lancTableCountLbl.setText(bundle.getString("lancamento.table.one"));
		}else{
			lancTableCountLbl.setText(MessageFormat.format(bundle.getString("lancamento.table.many"), itemCount));
		}
	}


	protected Color getColor(int[] rgb){
		return new Color(display,rgb[0],rgb[1],rgb[2]);
	}

	private void fillData() {
		lancTable.removeAll();

		java.util.List<LancamentoTableData> data = lancamentoController.getData();
		TableItem item = null;

		for(int i = 0; i<data.size(); i++){
			item = new TableItem(lancTable, SWT.NONE);
			LancamentoTableData lancData = data.get(i);
			
			if(lancData.saldo){
				item.setImage(balanceArrowIcon);
			}else{
				if(lancData.receita){
					item.setImage(receitaImage);
				}else{
					item.setImage(despesaImage);
				}
			}

			item.setBackground(getColor(lancData.background));
			item.setForeground(getColor(lancData.foreground));
			item.setText(lancData.toArray());
		}
		updateCountLabel();

		cbMonth.select(periodoController.getSelectedMonth());
		cbYear.select(periodoController.getSelectedYear());

		boolean finalizados = lancamentoController.isLancamentosFinalizados();

		liberarSaldoItem.setEnabled(finalizados);
		gerarSaldoItem.setEnabled(!finalizados);

	}


	public void run() {

		display = Display.getDefault();
		
		applicationUtil = new ApplicationUtil();
		
		Display.setAppName(applicationUtil.getName());
		Display.setAppVersion(applicationUtil.getVersion());
		
		shell = new Shell(display);
		shell.setLayout(new FormLayout());
		shell.setText(applicationUtil.getName() + " " + applicationUtil.getVersion());
		
		bundle = ResourceBundle.getBundle("messages", locale);

		initializeResourceImages();
		initializeEvents();
		try {
			initalizeContexts();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		initMenu();
		initToolBar();
		initSecondToolBar();

		initTable();

		root.pack();
		shell.pack();

		shell.setMaximized(true);
		shell.open();

		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}

	}

	public static void main(String[] args) throws NamingException {
		new Thread(new ApplicationRunner()).run();
	}

	@Override
	public void changed(ServiceChangeEvent event) {
		if(event.service instanceof ContaService){
			
			ContaService contaService = (ContaService) event.service;
			refreshContasCombo(contaService);
			
		}else{
			fillData();
		}
	}

}